package com.xxx.obj;

import java.io.*;

/**
 * @version v1.0
 * @Description
 * @Author admin
 * @date 2021/7/23  9:52
 * @Copyright
 */
public class objectStream {
    public static void main(String[] args) {
        write();
        try {
            ObjectInputStream stream = new ObjectInputStream(new FileInputStream("a.txt"));
            Student student = (Student) stream.readObject();
            System.out.println("student = " + student);
            System.out.println(student.sex);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    }

    private static void write() {
        try (
                ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream("a.txt"))

        ) {
            final Student student = new Student("张三",22);
            student.sex = "男";
            stream.writeObject(student);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
